classdef FirstOrderPoly < handle
    %FirstOrderPoly Plots a first order polynomial
    %   Example of using a gui within a class
    
    properties
        A           %Y intercept
        B           %Slope
        Xstart=0    %Plot Range Start
        Xend=100    %Plot Range End
        Xint=1      %Plot Interval Step Size
        Xvec        %X vector for plotting
        Y           %Y data for plotting
    end
    
    methods
        
        %A simple function for plotting
        function plotLine(obj,IsGUICall)
            if nargin<2
                IsGUICall=0;
            end
            if ~IsGUICall %Then we need to create a new figure
                figure
            end
            
            obj.genData();
            plot(obj.Xvec,obj.Y)
            xlabel('X')
            ylabel('Y')
        end
        
        function genData(obj)
            obj.Xvec=(obj.Xstart:obj.Xint:obj.Xend);
            obj.Y=obj.A+obj.B*obj.Xvec;
        end
        
        %This is the function that sets up our GUI
        function gui(obj)
            
            %Create figure and position on the screen
            %Position is [left bottom width height]
            FigH=figure('Position',[100 100 600 400])
            FigH.CloseRequestFcn=@myclose;
            
            %Create axis for our plot
            AxH=axes('Units','Pixels')
            %Set axis position
            AxH.Position = [150 50 400 300]
            
            %Setup our edit boxes
            EditTxtA = uicontrol('Style','edit',...
                'Position',[20 350 50 20],...
                'String','A');
            
            EditTxtB = uicontrol('Style','edit',...
                'Position',[20 300 50 20],...
                'String','B');
            
            
            %Setup pushbutton
            PushButton = uicontrol('Style','pushbutton',...
                'Position',[20 250 50 20],...
                'String','plot');
            PushButton.Callback=@myplot;
            
            %Put object properties (A and B) into edit boxes
            params2gui()
            
   
            %This function is called when figure is closed
            function myclose(~,~) %(~,~) is required to catch two inputs
                gui2params();
                delete(FigH)
            end
            
            %Update object properties with GUI values. 
            function gui2params()
                obj.A=str2double(EditTxtA.String);
                obj.B=str2double(EditTxtB.String);
            end
            
            %Update GUI with objecte properties.
            function params2gui()
                EditTxtA.String=num2str(obj.A)
                EditTxtB.String=num2str(obj.B)
            end
            
            %This function is called when we hit the 'plot' pushbutton
            function myplot(~,~) %(~,~) is required to catch two inputs
                gui2params();
                obj.genData();
                obj.plotLine(1);    
            end
            
            
        end
    end
    
end

